"""
Author: runyu.ma

flatten_comma_pass.py
"""

from ..core.dslc_parser import BinaryOp, BlockOp, ASTNode


def flatten_nested_binary_comma(_, top: BinaryOp):
    if top.op != ',':
        raise ValueError("not COMMA")
    oplist = []
    __lowering_comma(oplist, top)
    ret = BlockOp(oplist)
    return ret


def __lowering_comma(oplist, top):
    if isinstance(top.l, BinaryOp) and top.l.op == ',':
        __lowering_comma(oplist, top.l)
    else:
        oplist.append(top.l)
    #
    if isinstance(top.r, BinaryOp) and top.r.op == ',':
        __lowering_comma(oplist, top.r)
    else:
        oplist.append(top.r)

